package cn.tedu.store.product.webapi.service.impl;

import cn.tedu.store.commons.pojo.product.dto.AlbumAddNewDTO;
import cn.tedu.store.commons.pojo.product.dto.AlbumUpdateDTO;
import cn.tedu.store.commons.pojo.product.entity.Album;
import cn.tedu.store.commons.pojo.product.vo.AlbumListItemVO;
import cn.tedu.store.commons.pojo.product.vo.AlbumStandardVO;
import cn.tedu.store.commons.ex.ServiceException;
import cn.tedu.store.product.webapi.mapper.AlbumMapper;
import cn.tedu.store.product.service.IAlbumService;
import cn.tedu.store.commons.web.ServiceCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service
public class AlbumServiceImpl implements IAlbumService {

    @Autowired
    AlbumMapper albumMapper;
    @Override
    public void addNew(AlbumAddNewDTO albumAddNewDTO) {
        log.debug("开始处理添加相册业务 参数:{}",albumAddNewDTO);
        int count = albumMapper.countByName(albumAddNewDTO.getName());
        if(count != 0){
            String message = "添加失败,相册名称已被占用!";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_CONFLICT, message);
        }
        log.debug("准备添加相册");
        Album album = new Album();
        BeanUtils.copyProperties(albumAddNewDTO,album);
        int rows = albumMapper.insert(album);
        if(rows != 1){
            String message = "操作失败,服务器忙,请稍后再试!";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_INSERT,message);
        }
    }

    @Override
    public void delete(Long id) {
        log.debug("开始处理[删除相册]业务 参数:{}",id);
        AlbumStandardVO queryResult = albumMapper.getStandardById(id);
        if(queryResult == null){
            String message = "删除相册失败,要访问的资源不存在!";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND,message);
        }
        int rows = albumMapper.deleteById(id);
        if(rows != 1){
            String message = "操作失败,服务器忙,请稍后再试!";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_DELETE,message);
        }
    }

    @Override
    public void update(Long id, AlbumUpdateDTO albumUpdateDTO) {
        log.debug("开使处理修改相册的业务 参数:{}",albumUpdateDTO);
        AlbumStandardVO queryResult = albumMapper.getStandardById(id);
        if(queryResult == null){
            String message = "修改相册失败,访问的资源不存在!";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND,message);
        }
        Album album = new Album();
        BeanUtils.copyProperties(albumUpdateDTO,album);
        album.setId(id);
        int rows = albumMapper.updateById(album);
        if(rows != 1){
            String message = "操作失败,服务器忙,请稍后再试!";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_UPDATE,message);
        }

    }

    @Override
    public List<AlbumListItemVO> list() {
        log.debug("开始处理[获取相册列表]请求 参数:无");
        return albumMapper.list();
    }


}
